import { PathController, ProduciblePath } from '@baseController/PathController'
import { SaveAble } from '@baseController/SaveController'

export interface PageInfo {
  [key: string]: {
    window: {
      usingComponents?: { [key: string]: unknown }
      [key: string]: unknown
    }
  }
}

type PageResult = Array<[ProduciblePath, SaveAble]>

export function createPage(path: ProduciblePath, data: PageInfo[string]): PageResult {
  const pCtrl = PathController.make(path)
  const unixpath = pCtrl.whitout().unixpath
  const window = data.window
  const isComponent = Boolean(window.component)
  const LICENSE = 'Generated by @unveilr'
  const fileContents = {
    wxml: `<!--${LICENSE}-->\n<text>${unixpath}</text>`,
    json: window,
    js: `/* ${LICENSE} */\n${isComponent ? 'Component' : 'Page'}({})`,
    wxss: `/* ${LICENSE} */`,
  }
  return Object.entries(fileContents).map(([suffix, buffer]) => [pCtrl.whitout(`.${suffix}`), buffer])
}
